package com.control 
{
	import com.data.GameData;
	import com.Dlg.DlgManager;
	import com.event.EventManager;
	import com.event.PublicEvent;
	import com.greensock.TweenLite;
	import com.item.Bells;
	import com.item.BellsView;
	import com.item.Bird;
	import com.item.BirdsView;
	import com.item.Rabbit;
	import com.item.Snow;
	import com.item.SnowView;
	import com.sound.SoundCtrl;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author wen
	 */
	public class Controler
	{
		//重力
		private const GRAVITY:Number = 1.5;
		//x方向移动速度
		private const X_SPEED:Number = 10;
		//每一次的跳起初速度
		private const Y_INIT_SPEED:Number = -28;
		//y方向的速度
		private var _ySpeed:Number = 0;
		//是否在空中
		private var _isInSky:Boolean = false;
		//初始位置
		private var _posArr:Array = null;
		//玩家
		private var _rabbit:Rabbit = null;
		//总mc
		public var view:MovieClip = null;
		
		private var _map:MovieClip = null;
		
		private var _bellsView:BellsView = null;
		
		private var _birdsView:BirdsView = null;
		
		private var _snowsView:SnowView = null;
		
		private var _score:int = 0;
		
		private var _totalScore:int = 0;
		
		private var _snowNum:int = 0;
		
		private var _bgSwitch:BgSwitch;
		
		public function Controler(mc:MovieClip) 
		{
			view = mc;
			init();
		}
		
		/**
		 * 初始化
		 */
		private function init():void
		{
			_map = view.mapArea;
			_rabbit = new Rabbit(_map.rabbitMc);
			
			GameData.rabbit = _map.rabbitMc;
			
			view.score.label.text = _totalScore;
			view.score.y = 115;
			
			_posArr = [_rabbit.view.x,_rabbit.view.y];
			
			_bellsView = new BellsView();
			_bellsView.init(_map);
			
			_birdsView = new BirdsView();
			_birdsView.init(_map);
			
			_snowsView = new SnowView();
			_snowsView.init(_map);
			
			_bgSwitch = new BgSwitch(_map.bg0, _map.bg1);
			
			SoundCtrl.instance.soundPlay("bg");
			
			EventManager.instance.addEventListener(PublicEvent.RESTART, onRestartHander);
		}
		
		private function onRestartHander(e:PublicEvent):void 
		{
			restart();
		}
		
		private function restart():void 
		{
			clear();
			
			_bellsView.initItem();
			_birdsView.initItem();
		}
		
		//入帧处理
		private function onGameHander(e:Event):void 
		{
			rabbitMove();
			
			rabbitHighTest();
			
			bellsCheck();
			
			endCheck();
			
			showSnow();
			
			_bgSwitch.check();
		}
		
		private function showSnow():void 
		{
			_snowsView.fallCheck();
		}
		
		private function endCheck():void 
		{
			var tmpList:Array = _bellsView.hasAddList;
			if (GameData.isStart&&_score>0)
			{
				if (_rabbit.view.y == _posArr[1])
				{
					trace("--------------------------------------------end");
					DlgManager.instance.endDlg.show( { score:_totalScore } );
					GameData.isStart = false;
				}
			}
		}
		
		private function bellsCheck():void 
		{
			var tmpList:Array = _bellsView.hasAddList;
			for (var i:int = 0; i < tmpList.length; i++)
			{
				//var bell:Bells = _bellsView.hasAddList[_bellsView.hasAddList.length - 1] as Bells;
				//if (_rabbit.view.y - bell.view.y < 300)
				//{
					//_bellsView.addClildBell();
				//}
				
				if ((tmpList[i] as Bells).view.y - _rabbit.view.y > 500)
				{
					_bellsView.deleteBell(i);
				}
			}
			
			var birdList:Array = _birdsView.hasAddList;
			for (var j:int = 0; j < birdList.length; j++)
			{
				if ((birdList[j] as Bird).view.y - _rabbit.view.y > 500)
				{
					_birdsView.deleteBird(j);
				}
			}
		}
		
		//处理兔子和铃铛变化
		private function rabbitHighTest():void 
		{
			
			var tmpList:Array = _bellsView.hasAddList;
			for (var i:int = 0; i < tmpList.length; i++)
			{
				if (_rabbit.view.hitTestObject((tmpList[i] as Bells).view.bell) )
				{
					_rabbit.setState("jump");
					_ySpeed = Y_INIT_SPEED;
					_score += 10;
					_totalScore += _score;
					view.score.label.text = _totalScore;
					
					_bellsView.currentBell = {num:i,score:_score};
					
					trace(_rabbit.view.y+"             "+_bellsView.hasAddList.length);
				}
			}
			
			var birdList:Array = _birdsView.hasAddList;
			for (var j:int = 0; j < birdList.length; j++)
			{
				if (_rabbit.view.hitTestObject((birdList[j] as Bird).view.a) )
				{
					_rabbit.setState("jump");
					_ySpeed = Y_INIT_SPEED;
					
					_totalScore = _totalScore*2;
					view.score.label.text = _totalScore;
					
					_birdsView.currentBird = { num:j };
					
					
				}
			}
		}
		
		/**
		 * 玩家操作兔子
		 */
		private function rabbitMove():void 
		{	
			rabbitXChange();
			
			if (_rabbit.view.x < 11)
			{
				_rabbit.view.x = 11;
			}
			
			if (_rabbit.view.x > (GameData.gameWidth - 16))
			{
				_rabbit.view.x = GameData.gameWidth-16;
			}
			
			_ySpeed +=  GRAVITY;
			_rabbit.view.y += _ySpeed;
			if (_rabbit.view.y >= _posArr[1])
			{
				_rabbit.view.y = _posArr[1];
				_isInSky = false;
			}
			
			
			if (_rabbit.view.y < _posArr[1] - 270)
			{
				_map.y = -(_rabbit.view.y - _posArr[1] + 270) ;
			}
			else
			{
				_map.y = 0;
			}
		}
		
		private function rabbitXChange():void 
		{
			if (!_isInSky)
			{
				if (Math.abs(_rabbit.view.x - GameData.stage.mouseX)>X_SPEED)
				{
					if (_rabbit.view.x > GameData.stage.mouseX)
					{
						_rabbit.view.x -= X_SPEED;
						_rabbit.view.scaleX = 1;
					}
					else
					{
						_rabbit.view.x += X_SPEED;
						_rabbit.view.scaleX = -1;
					}
					_rabbit.setState("run");
				}
				else
				{
					//_rabbit.view.x = view.stage.mouseX;
					_rabbit.setState("sit");
				}
			}
			else
			{
				_rabbit.view.x += (GameData.stage.mouseX - _rabbit.view.x) * .12;
				if (Math.abs(_rabbit.view.x - GameData.stage.mouseX)>1)
				{
					if (_rabbit.view.x > GameData.stage.mouseX)
					{
						_rabbit.view.scaleX = 1;
					}
					else
					{
						_rabbit.view.scaleX = -1;
					}
				}
			}
		}
		
		
		//鼠标事件
		private function onClickEvent(e:MouseEvent):void 
		{
			switch(e.type)
			{
				case MouseEvent.CLICK:
					jump();
					break;
			}
		}
		
		//起跳
		private function jump():void
		{
			if (!_isInSky&&GameData.isStart)
			{
				_rabbit.setState("jump");
				_ySpeed = Y_INIT_SPEED;
				_isInSky = true;
			}
		}
		
		private function clear():void
		{
			_isInSky = false;
			_totalScore = 0;
			_score = 0;
			_map.y = 0;
			
			_rabbit.view.x = _posArr[0];
			_rabbit.view.y = _posArr[1];
			
			_bellsView.clear();
			_birdsView.clear();
			
			view.score.label.text = "" + 0;
			
			_bgSwitch.init();
		}
		
		public function show():void 
		{
			GameData.stage.addEventListener(Event.ENTER_FRAME, onGameHander);
			GameData.stage.addEventListener(MouseEvent.CLICK, onClickEvent);
			
			restart();
			
			view.visible = true;
		}
		
		public function hide():void 
		{
			view.visible = false;
			GameData.stage.removeEventListener(Event.ENTER_FRAME, onGameHander);
			GameData.stage.removeEventListener(MouseEvent.CLICK, onClickEvent);
			
			clear();
		}
	}

}